//
//  Client.cpp
//  myhttpserver
//
//  Created by tgy on 2018/3/16.
//  Copyright © 2018年 tgy. All rights reserved.
//

#include "Client.hpp"
#include <string.h>
#include <arpa/inet.h>
SendMessageItem::SendMessageItem(){
    
    this->conten = NULL;
    this->len = 0;
    this->sendCurrent = 0;
}

SendMessageItem::SendMessageItem(const char *conten,size_t len){
    
    if (len > 0) {
        
        this->conten = new char[len];
        this->len = (int)len;
        bzero(this->conten, 0);
        strcpy(this->conten, conten);
    }else{
        
        this->conten = NULL;
        this->len = 0;
    }
    this->sendCurrent = 0;
}

SendMessageItem::~SendMessageItem(){
    
    if (this->conten != NULL) {
        
        delete [] this->conten;
        this->conten = NULL;
        this->len = 0;
    }
    
}

char * SendMessageItem::getConten(){
    
    return this->conten;
}

int SendMessageItem::getLen(){
    
    return this->len;
}

void SendMessageItem::setSendCurrent(int sendCurrent){
    
    if (sendCurrent < 0) {
        
        return;
    }
    
    this->sendCurrent = sendCurrent;
}
int SendMessageItem::getSendCurrent(){
    
    return this->sendCurrent;
}

Client::Client(int fd, struct sockaddr_in s_in){
    
    this->fd = fd;
    this->s_in = s_in;
    
    //获取客户端ip
   clientIP =  inet_ntoa(s_in.sin_addr);
    //获取连接客户端口号
    port = ntohs(s_in.sin_port);
}

std::string Client::getClientIP(){
    
    return clientIP;
}

int Client::getClientPort(){
    
    return port;
}

Client::~Client(){
    
    
}

void Client::pushSendItem(SendMessageItem *smi){
 
    this->sendMessage.push(smi);
}
SendMessageItem * Client::popSendItem(){
    
    if (this->sendMessage.empty()) {
        
        return NULL;
    }
    SendMessageItem * top = fontSendItem();
    this->sendMessage.pop();
     return  top;
}

SendMessageItem * Client::fontSendItem(){
    
    if (this->sendMessage.empty()) {
        
        return NULL;
    }
    
    return this->sendMessage.front();
}
